winsafe\gui\native_controls/
header_items.rs

1use crate::co;
2use crate::decl::*;
3use crate::gui::{iterators::*, *};
4use crate::msg::*;
5use crate::prelude::*;
6
7/// Exposes the item methods of a [`Header`](crate::gui::Header) control.
8///
9/// You cannot directly instantiate this object, it is created internally by the
10/// control.
11pub struct HeaderItems<'a> {
12	owner: &'a Header,
13}
14
15impl<'a> HeaderItems<'a> {
16	#[must_use]
17	pub(in crate::gui) const fn new(owner: &'a Header) -> Self {
18		Self { owner }
19	}
20
21	/// Adds a new item by sending an
22	/// [`hdm::InsertItem`](crate::msg::hdm::InsertItem) message, returning the
23	/// new item.
24	pub fn add(&self, text: &str, width: i32) -> SysResult<HeaderItem<'a>> {
25		let mut hdi = HDITEM::default();
26		hdi.mask = co::HDI::TEXT | co::HDI::WIDTH;
27		hdi.cxy = width;
28
29		let mut wtext = WString::from_str(text);
30		hdi.set_pszText(Some(&mut wtext));
31
32		let idx = unsafe {
33			self.owner
34				.hwnd()
35				.SendMessage(hdm::InsertItem { index_after: 0xffff, item: &hdi })?
36		};
37		Ok(self.get(idx))
38	}
39
40	/// Retrieves the total number of items by sending a
41	/// [`hdm::GetItemCount`](crate::msg::hdm::GetItemCount) message.
42	#[must_use]
43	pub fn count(&self) -> SysResult<u32> {
44		unsafe { self.owner.hwnd().SendMessage(hdm::GetItemCount {}) }
45	}
46
47	/// Retrieves the item at the given zero-based position.
48	///
49	/// **Note:** This method is cheap – even if `index` is beyond the range of
50	/// existing items, an object will still be returned. However, operations
51	/// upon this object will produce no effect.
52	#[must_use]
53	pub const fn get(&self, index: u32) -> HeaderItem<'a> {
54		HeaderItem::new(self.owner, index)
55	}
56
57	/// Returns an iterator over all items.
58	#[must_use]
59	pub fn iter(&self) -> SysResult<impl DoubleEndedIterator<Item = HeaderItem<'a>> + 'a> {
60		HeaderItemIter::new(self.owner)
61	}
62
63	/// Returns the last item, if any.
64	pub fn last(&self) -> SysResult<Option<HeaderItem<'a>>> {
65		let count = self.count()?;
66		Ok(if count > 0 { Some(self.get(count - 1)) } else { None })
67	}
68}